<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use DB;
use Crypt;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class UserController extends Controller
{
    //add
    public function add()
    {
    	return view('admin.user.add');
    }

    //insert
    public function insert(Request $request)
    {
    	// dd($request -> all());
    	
    	// if(!$request -> input['name'])
    	// {
    	// 	return back() -> with(['name' => '用户名不能为空']);
    	// }

    	$this -> validate($request,[
    		'name' => 'required',
    		'password' => 'required',
    		'repassword' => 'required|same:password',
    		'email' => 'required|email|unique:users',
    		'phone' => 'required|max:11|min:11'


    		],[
    		'name.required' => '用户名不能为空',
    		'password.required' => '密码不能为空',
    		'repassword.required' => '确认密码不能为空',
    		'repassword.same' => '两次密码不一致',
    		'email.required' => '邮箱不能为空',
    		'email.email' => '邮箱格式不正确',
    		'email.unique' => '邮箱地址已存在',
    		'phone.required' => '手机号不能为空',
    		'phone.max' => '手机号必须是11位',
    		'phone.min' => '手机号必须是11位'
            

    	]);

    	//处理好所有数据
    	$data = $request -> except('_token','repassword');
    	// dd($data);

    	

    	//加密密码
    	$data['password'] = Crypt::encrypt($data['password']);
    	// dd($data);

    	//解密示例
    	// $a = "eyJpdiI6IktcL0Q4M3YyakJNclIyTlIzN041eXBRPT0iLCJ2YWx1ZSI6IkFoWG5nbjhMS3NBRURHbDJqMGtURHc9PSIsIm1hYyI6IjM5NTEzZGJlYmYwZWQ0NmI3ZDMwNTAxM2FlOWJjOTlkNzU1Mjk4MDdkYzViOTE0YzJiMDU0OGNkYWRjMzU3YjQifQ==";

    	// $A = Crypt::decrypt($a);
    	// dd($A);
    	
    	$data['remember_token'] = str_random(70);
    	$time = date('Y-m-d H:i:s');
    	$data['created_at'] = $time;
    	$data['updated_at'] = $time;
    	// dd($data);

    	//添加数据
    	$res = DB::table('users') -> insert($data);
    	// dd($res);
    	// $res = DB::table('users') -> insert($data);

    	// $res = true;


    	if($res)
    	{
    		// return ('添加成功');
    		return redirect('/admin/user/index') -> with(['info' => '添加成功']);
    	}else
    	{
    		// return ('添加失败');
    		return redirect('/admin/user/add') -> with(['info' => '添加失败']);
    	}

    }

    //index
    public function index(Request $request)
    {

    	// dd($request);
    	//查询数据库
    	$num = $request -> input('num',10);


    	
    	$data = DB::table('users') 
    		-> where(function($query) use ($request){
    			$query -> where('name', 'like', '%'.$request -> input('keyword').'%');
    		})
    		-> paginate($num);

    		// dd($request -> all());
 
    	return view('/admin/user/index',['data' => $data, 'request' => $request -> all() ]) ;
    }


    //编辑
    public function edit($id)
    {
    	//查询数据库
    	$data = DB::table('users') -> where('id',$id) -> first();

    	return view('admin.user.edit',['data' => $data] );
    }

    //更新
    public function update(Request $request)
    {

    	$this -> validate($request,[
    		'name' => 'required',
    		'email' => 'required|email|unique:users',
    		'phone' => 'required|max:11|min:11'
    		],[
    		'name.required' => '用户名不能为空',
    		'email.required' => '邮箱不能为空',
    		'email.email' => '邮箱格式不正确',
    		'email.unique' => '邮箱地址已存在',
    		'phone.required' => '手机号不能为空',
    		'phone.max' => '手机号必须是11位',
    		'phone.min' => '手机号必须是11位'
    		]);
    	// dd($request -> all());
    	$data = $request -> except('_token','id');
    	// return $data;
    	
    	$data['updated_at'] = date('Y-m-d H:i:s');

    	//执行更新
    	$res = DB::table('users') -> where('id',$request -> input('id')) -> update($data);

    	// dd('111');

    	//判断
    	if($res)
    	{
    		return redirect('/admin/user/index') -> with(['info' => '更新成功']);
    	}else
    	{
    		return back() -> with(['error' => '更新失败']);
    	}
    }

    //删除
    public function delete($id)
    {
    	// return $id;
    	
    	$res = DB::table('users') -> delete($id);

    	//判断
    	if($res)
    	{
    		return redirect('/admin/user/index') -> with(['info' => '删除成功']);
    	}else
    	{
    		return back() -> with(['error' => '删除失败']);
    	}
    }

    //ajaxUpdate
    public function ajaxUpdate(Request $request)
    {
    	// return response() -> json(0);
    	$id = $request -> input('id');
    	//根据ID查询数据
    	$data = DB::table('users') -> where('id', $id) -> first();

    	//判断
    	if($data -> status == '0')
    	{
    		$res = DB::table('users') -> where('id', $id) -> update(['status' => '1']);
    		if($res)
    		{
    			return response() -> json(0);
    		}else
    		{
    			return response() -> json(1);
    		}
    	}else
    	{
    		$res = DB::table('users') -> where('id', $id) -> update(['status' => '0']);
    		if($res)
    		{
    			return response() -> json(2);
    		}else
    		{
    			return response() -> json(3);
    		}
    	}


    }

    //ajaxUsername
    public function ajaxUsername(Request $request)
    {
    	$name = $request -> input('name');
    	//查询
    	$res = DB::table('users') -> where('name',$name) -> first();
    	if($res)
    	{
    		//1代表用户名已存在
    		return response() -> json(1);
    	}else
    	{
    		$res = DB::table('users') -> where('id',$request -> input('id')) -> update(['name' => $name]);

    		if($res)
    		{
    			return response() -> json(0);
    		}else
    		{
    			return response() -> json(2);
    		}
    	}

    }

    //member
    public function member(Request $request)
    {
        // dd($request -> all());
        //查询数据库
        $num = $request -> input('num',10);


        $data = DB::table('users')
            ->join('users_detail', 'users.id', '=', 'users_detail.users_id')
         
            ->select('users.*', 'users_detail.*')
            -> where(function($query) use ($request){
                $query -> where('email', 'like', '%'.$request -> input('keyword').'%');
            })
            -> paginate($num);
        

        // dd($data );
        return view('/admin/user/member',['data' => $data,  'request' => $request -> all() ]) ;
        // return view('admin.user.member');
    }

    //member_level
    public function member_level()
    {
        return view('admin.user.member_level');
    }

    //add_level
    public function add_level()
    {
        return view('admin.user.add_level');
    }
}
